/*
 * Created by Federico Pisarello - SisDev Software (c) 2012 - fpisarello@sisdevsoft.com
 */


var actionForm = 'tablesAction.php';
var popForm = 'tableState.php';
var popPDF = 'tablePrintOrder.php';
var actionPK = undefined;

function $_refreshTables()
{
		
	paramData = "actionOfForm=getTableState";
	
	$.ajax({
	    type: 'POST',
	    url: actionForm,
	    data: paramData,
		dataType: 'json',
	    success: function(jsonObj)
	                        {
	    						if(jsonObj.length !== 0)
	    						{
	    							$.each(jsonObj.tables, function(i, table) {
		    							if(table.state == 'U' || table.state == 'C')
		    							{
		    								if(table.state == 'U') {
		    									$('#'+table.id).children('.widget-head').attr('title', tableStateDescriptionBusy);
		    									$('#'+table.id).children('.widget-head').removeClass('tableOpen tableClose').addClass('tableBusy');
		    								}
		    								else {
		    									$('#'+table.id).children('.widget-head').attr('title', tableStateDescriptionClose);
		    									$('#'+table.id).children('.widget-head').removeClass('tableBusy tableOpen').addClass('tableClose');
		    								}
		    								$('#'+table.id).children('.widget-content').empty();
		    								$('#'+table.id).children('.widget-content').append($('<p>').text(table.guests).css('font-weight', 'bold'));
		    								$.each(table.items, function(i, item) { 
		    									$('#'+table.id).children('.widget-content').append($('<p>').text(item.typeFoodDescription));
		    								});
		    								$('#'+table.id).children('.widget-content').append($('<p>').text(table.total).css('text-align', 'right').css('font-weight', 'bold'));
		    								if(table.items.length>0) {
		    									$('#'+table.id).children('.widget-content').append($('<button id="v'+table.id + '" class="itmIngBtnIn">').text(viewDetail).addClass('viewTable'));
		    									$_InitButton();
		    								}
		    							}
		    							else
										{
		    								$('#'+table.id).children('.widget-head').attr('title', tableStateDescriptionOpen);
		    								$('#'+table.id).children('.widget-head').removeClass('tableBusy tableClose').addClass('tableOpen');
		    								$('#'+table.id).children('.widget-content').empty();
		    								$('#'+table.id).children('.widget-content').append($('<p>').text(table.description));
										}
	    							});
	    						}
	                        }
    });
}

function $_Init()
{
	var refreshId = setInterval( function()    
			{        
				$_refreshTables();    
			}, 5000);
	
	$("#tableStateDialog").dialog({
		autoOpen:false, 
		height:380, 
		width:600, 
		dialogClass:'no-close',
		beforeClose: function() {$("#tableStateDialog").empty();},
		open: function() {
			$('#tableStateDialog').css('background','url(imagenes/bg.gif)');
			//Add Click for All Buttons in Pop Form
		}
	});
	
	$_InitButton();
}

function $_InitButton()
{
	$('.viewTable').click( function (){
		//Reset State of action and IndexPK
		actionPK = $(this).attr('id').replace("vidTable","");
		$('#tableStateDialog').load(popForm).dialog('open');
		return false;
	});
}

function $_InitPop()
{
	$(document).ready(function() {
		if($('#idTable'+actionPK).children('.widget-head').hasClass("tableClose"))
			$('#btnOpenTable').show();
		else
			$('#btnOpenTable').hide();
		
    	oTable = $('#dt_tableState').dataTable( {
					"bJQueryUI": true,
					"sPaginationType": "two_button",
					"bInfo": false,
					"bLengthChange": false,
					"bProcessing": true,
					"bServerSide": true,
					"bFilter": false,
					"sAjaxSource": actionForm + "?actionOfForm=getDetailTableState&idTable=" + actionPK,
					"aoColumns": [
					            null,
					            {
					            	"sType": "numeric",
					            	"sClass": "textAlignRight"
					            },
					  			{
					            	"sType": "numeric",
					            	"sClass": "textAlignRight",
					            	"fnRender": function (oObj) {    
					            		return RenderDecimalNumber(oObj);
					            	}
					            }
					  			]
				});
	});
	
	$('#btnClose').click( function (e) {
		$('#tableStateDialog').dialog('close');
	});
	
	$('#btnOpenTable').click( function (e) {
		var paramData =  "actionOfForm=" + iConst.OPEN_TABLE + "&idTable=" + actionPK;
		
		 $.ajax({
		        type: 'POST',
		        url: actionForm,
		        data: paramData,
				dataType: 'json',
		        success: function(jsonObj)
		                            {
		        						if(jsonObj.status === 'OK') {
		        							$('#idTable'+actionPK).children('.widget-head').attr('title', tableStateDescriptionOpen);
		        							$('#idTable'+actionPK).children('.widget-head').removeClass('tableBusy tableClose').addClass('tableOpen');
		    								$('#idTable'+actionPK).children('.widget-content').empty();
		    								$('#idTable'+actionPK).children('.widget-content').append($('<p>').text(''));
		    								$('#tableStateDialog').dialog('close');
		        						}
		                            }
       });
	});
	
	$('#btnPrint').click( function (e) {
		var paramData = popPDF + "?idTable=" + actionPK;
		window.open(paramData,'mywindow','width=400,height=200');
	});
}

function RenderDecimalNumber(oObj) {
	var num = new NumberFormat();    
	num.setInputDecimal('.');    
	num.setNumber(oObj.aData[oObj.iDataColumn]);     
	num.setPlaces(2, true);        
	num.setCurrency(true);     
	num.setNegativeFormat(num.LEFT_DASH);     
	return num.toFormatted();
}